package com.pureut.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.DateUtils;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.equipment.domain.EquipmentKnowledge;
import com.pureut.equipment.domain.dto.EquipmentKnowledgeDto;
import com.pureut.equipment.domain.vo.EquipmentKnowledgeVo;
import com.pureut.equipment.mapper.EquipmentKnowledgeMapper;
import com.pureut.equipment.service.IEquipmentKnowledgeService;
import com.pureut.system.api.FeignService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @Date 23-04-23
 * @Author hl
 **/
@Service
public class EquipmentKnowledgeServiceImpl extends ServiceImpl<EquipmentKnowledgeMapper, EquipmentKnowledge> implements IEquipmentKnowledgeService {

    @Resource
    private EquipmentKnowledgeMapper equipmentKnowledgeMapper;

    @Resource
    private FeignService feignService;

    /**
     * 根据设备知识库列表
     *
     * @param equipmentKnowledgeVo 接受类
     * @return 设备知识库列表
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<EquipmentKnowledgeDto> selectKnowledgeList(EquipmentKnowledgeVo equipmentKnowledgeVo) {
        return equipmentKnowledgeMapper.selectKnowledgeList(equipmentKnowledgeVo);
    }

    /**
     * 新增设备知识库
     *
     * @param equipmentKnowledge
     * @return 设备知识库新增
     */
    @Override
    public boolean insertEquipmentKnowledge(EquipmentKnowledge equipmentKnowledge) {
        equipmentKnowledge.setCreateTime(DateUtils.getTime());
        equipmentKnowledge.setCreateBy(SecurityUtils.getUsername());
        equipmentKnowledge.setDeptId(String.valueOf(SecurityUtils.getDeptId()));
        String authorityCoding = feignService.getAuthorityCoding("device:knowledge:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        equipmentKnowledge.setKnowledgeCode(authorityCoding);
        return save(equipmentKnowledge);
    }

    /**
     * 修改设备知识库
     *
     * @param equipmentKnowledge
     * @return 设备知识库修改
     */
    @Override
    public boolean updateEquipmentKnowledge(EquipmentKnowledge equipmentKnowledge) {
        return updateById(equipmentKnowledge);
    }

    /**
     * 查询单个设备知识库
     *
     * @param knowledgeId id
     * @return 查询单个
     */
    @Override
    public EquipmentKnowledgeDto selectKnowledgeById(long knowledgeId) {
        EquipmentKnowledgeVo vo = new EquipmentKnowledgeVo();
        vo.setKnowledgeId(knowledgeId);
        return equipmentKnowledgeMapper.selectKnowledgeList(vo).get(0);
    }

    /**
     * 删除单个设备知识库
     *
     * @param knowledgeId id
     * @return 删除单个
     */
    @Override
    public boolean deleteKnowledgeById(long knowledgeId) {
        return removeById(knowledgeId);
    }

    @Override
    public List<Map<String, String>> selectKnowledgeById1() {
        List<Map<String, String>> list = equipmentKnowledgeMapper.selectKnowledgeById1();
        return list;
    }



    //并首字母大写
    public static String upCaseFirstChar(String str) {
        char[] chars = str.toCharArray();
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            chars[0] = (char) (chars[0] - 32);
        }
        return new String(chars);
    }

}
